package zhoufu.sz.sanxiao_check.presents;

import com.google.gson.Gson;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.inject.RetrofitQualifier;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.SecureImgEntity;
import zhoufu.sz.sanxiao_check.models.SecureModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.BasicItem4Contract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 */
public class NewBasicItem4Presenter implements BasicItem4Contract.Presenter
{

    private BasicRequestRepository mRepository;

    private BasicItem4Contract.View view;

    private SecureModel _model;

    private SecureImgEntity entity;

    public int childIndex = 0;

    @Inject
    @RetrofitQualifier("Observable")
    Gson _gson;

    @Inject
    public NewBasicItem4Presenter(BasicRequestRepository mRepository)
    {
        this.mRepository = mRepository;
    }

    public void bidnView(BasicItem4Contract.View view)
    {
        this.view = view;
        mRepository.setGson(_gson);
    }


    public SecureModel get_model()
    {
        return _model;
    }

    @Override public void onStart()
    {
        mRepository.GetSecure(view.SID(), new RequestHelper.RequestCallBack<SecureModel>()
        {
            @Override public void onPullStart()
            {
                view.pullStart();
            }

            @Override public void onPullFailud(Throwable throwable)
            {
                view.pullFaild();
            }

            @Override public void onPullEnd()
            {
                view.pullEnd();
            }

            @Override public void onPullSuccessed(SecureModel s)
            {
                if ( s.isSuccess() )
                {
                    if ( s.getData().size() > 0 )
                    {
                        _model = s.getData().get(0);
                        view.setModel(_model);
                    }
                } else
                {
                    view.showToast(s.getErrMsg());
                }
            }
        });
    }


    public void showDelDialog(SecureImgEntity entity)
    {
        view.showDelDialog(entity);
    }


    public SecureImgEntity getEntity()
    {
        return entity;
    }

    @Override public void onPause()
    {

    }

    public void setEntity(SecureImgEntity entity)
    {
        this.entity = entity;
    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }

    @Override public void update(final SecureImgEntity entity)
    {
        mRepository.UpdateSecure(entity, view.SID(),
                new RequestHelper.RequestCallBack<IDModel>()
                {
                    @Override public void onPullStart()
                    {
                        view.pullStart();
                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        view.pullFaild();
                    }

                    @Override public void onPullEnd()
                    {
                        view.pullEnd();
                    }

                    @Override public void onPullSuccessed(IDModel s)
                    {
                        view.pullEnd();
                        if ( s.isSuccess() )
                        {
                            view.updateSuccese();
                            if ( s.getData().size() > 0 )
                            {
                                entity.PhotoID.set(s.getData().get(0).getID());
                            }
                        } else
                        {
                            view.showToast(s.getErrMsg());
                        }
                    }
                });
    }
}
